package components.contorls.superClass
{
    import components.core.UIComponent;
    
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    import mx.events.FlexEvent;

    [Event(name="selectionChange", type="mx.events.FlexEvent")]
    public class BaseCheckBox extends UIComponent
    {
        private var _label:String = "CheckBox";

        private var _selected:Boolean = false;

        public function BaseCheckBox()
        {
            super();
			checkButton.mouseEnabled=checkButton.mouseChildren=false;
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get labelTF():BaseLabel
        {
            return getChildByName("labelSkin") as BaseLabel;
        }
		
		public function get checkButton():BaseButton
		{
			return getChildByName("buttonSkin") as BaseButton;
		}

        public function get selectedDisplay():DisplayObject
        {
            return getChildByName("selectedSkin") as DisplayObject;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
			
			if (!_initialized)
			{
				labelTF.verticalCenter=0;
				labelTF.left=labelTF.x;
				labelTF.right=0;
				
				checkButton.verticalCenter=0;
			}
			
            if (labelTF)
                labelTF.text = label;
        }

        protected override function measureSizes():Boolean
        {
           var change:Boolean= super.measureSizes();
		   
		   if(change)
		   {
			   selectedDisplay.y=height/2-selectedDisplay.height/2;
		   }
		   
		   return change;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        [Inspectable(defaultValue="CheckBox")]
        public function get label():String
        {
            return _label;
        }

        public function set label(value:String):void
        {
            _label = value;

            invalidate();
        }

        [Inspectable(defaultValue="false")]
        public function get selected():Boolean
        {
            return _selected;
        }

        public function set selected(value:Boolean):void
        {
            _selected = value;
			
			if(selected)
				selectedDisplay.visible=true;
			else
				selectedDisplay.visible=false;
			
            dispatchEvent(new FlexEvent(FlexEvent.SELECTION_CHANGE));
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        protected override function frameConstructedHandler(event:Event):void
        {
            super.frameConstructedHandler(event);
			selectedDisplay.visible=false;
            selected = _selected;
        }

        override protected function mouseClickHandler(event:MouseEvent):void
        {
            super.mouseClickHandler(event);
            selected = !selected;
        }

		protected override function rollOverHandler(event:MouseEvent):void
		{
			super.rollOverHandler(event);
			checkButton.isOver=true;
			checkButton.setOverState();
		}
		
		protected override function rollOutHandler(event:MouseEvent):void
		{
			super.rollOutHandler(event);
			checkButton.isOver=false;
			checkButton.setOutState();
		}
		
		protected override function mouseDownHandler(event:MouseEvent):void
		{
			super.mouseDownHandler(event);
			checkButton.setDownState();
		}
		
		protected override function mouseUpHandler(event:MouseEvent):void
		{
			super.mouseUpHandler(event);
			checkButton.setUpState();
		}
		
        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;
            if (labelTF)
                labelTF.focusComp = focusComp;
        }

        public override function destroy():void
        {
            super.destroy();
            _label = "";
        }

        public override function validateNow():void
        {
            super.validateNow();
            labelTF.validateNow();
        }
    }
}